package com.CusterRelSys.control;

import com.CusterRelSys.entity.CstLost;
import com.CusterRelSys.service.ICstLostServiceDxy;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by AugusDuan on 2017/12/1.
 */

@Controller
@RequestMapping("/cstLost")
public class CstLostControlDxy {

    @Autowired
    ICstLostServiceDxy cstLostService;

    @RequestMapping("/showCstLostAll")
    public String showCstLostAll(CstLost cstLost, ModelMap modelMap,
                                 @RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "6") Integer pageSize){
        List <CstLost> cstLosts = cstLostService.showCstLostAll(cstLost,pageNum,pageSize);
        modelMap.addAttribute("cstLosts",cstLosts);
        return "forward:/~cust/lost/list.jsp";
    }


    @RequestMapping("/showCstLostAllAjax")
    @ResponseBody
    public List <CstLost> showCstLostAllAjax(CstLost cstLost,
                                             @RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "6") Integer pageSize,ModelMap modelMap){
        List <CstLost> cstLosts = cstLostService.showCstLostAll(cstLost,pageNum,pageSize);
        PageInfo<CstLost> page = new PageInfo <CstLost>(cstLosts);

        return cstLosts;
    }


    @RequestMapping("/showCstLostAllAjax1")
    public String showCstLostAllAjax1(CstLost cstLost,
                                      Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "6") Integer pageSize,
                                      ModelMap modelMap,
                                      HttpSession session){
        // pageNum == null,默认查询第一页，null,null,null
        if (pageNum==null){
            pageNum = 1;
            session.setAttribute("cstLost", cstLost);
        }else{
            cstLost = (CstLost) session.getAttribute("cstLost");
        }
        List <CstLost> cstLosts = cstLostService.showCstLostAll(cstLost,pageNum,pageSize);

        PageInfo<CstLost> pageInfo = new PageInfo <CstLost>(cstLosts);
        modelMap.addAttribute("pageInfo",pageInfo);
        return "~cust/lost/listCstLost";
    }

    @RequestMapping("/detailCstLost")
    public String detailCstLost(Integer lstId,ModelMap modelMap){
        CstLost cstLost = cstLostService.detailCstLostById(lstId);
        modelMap.addAttribute("cstLost",cstLost);
        return "~cust/lost/confirm";
    }


    @RequestMapping("/detailCstLostRelay")
    public String detailCstLostRelay(Integer lstId,ModelMap modelMap){
        CstLost cstLost = cstLostService.detailCstLostById(lstId);
        modelMap.addAttribute("cstLost",cstLost);
        return "~cust/lost/relay";
    }


    @RequestMapping("/insertCsLost")
    public String insertCsLost(CstLost cstLost){
        String lstDelay = cstLostService.detailCstLostById(cstLost.getLstId()).getLstDelay();
        if(lstDelay==null){
            lstDelay = cstLost.getLstDelay();
        }else {
            lstDelay = lstDelay +  cstLost.getLstDelay();
        }
        cstLost.setLstDelay(lstDelay);
        cstLostService.insertCsLost(cstLost);
        return "forward:/cstLost/showCstLostAll";
    }



    @RequestMapping("/insertCsLostReaason")
    public String insertCsLostReaason(CstLost cstLost){
        cstLost.setLstStatus("确定流失");
        cstLostService.insertCsLostReaason(cstLost);
        return "forward:/cstLost/showCstLostAll";
    }
}
